package jzqb.controller.admin;


import haha.life.common.CutPageBean;
import haha.life.common.MonitorTypeTool;
import haha.life.common.PermissionTypeTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import io.swagger.annotations.Api;
import jzqb.annotation.PermissionCheck.PermissionCheck;
import jzqb.bo.AdminQueryBo;
import jzqb.common.Result;
import jzqb.po.ZTJPo;
import jzqb.po.ZTPo;
import jzqb.po.ZTWXPo;
import jzqb.service.AdminTechnicalBagService;
import jzqb.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/adminTechnicalBag")
@Api(tags = "后台技术锦囊控制层")
public class AdminTechnicalBagController {

    @Autowired
    private AdminTechnicalBagService adminTechnicalBagService;

    @Value("${filePath.coverTemPResource}")
    private String coverTemPResource;

    @Value("${filePath.ztjCoverPath}")
    private String ztjCoverPath;

    @Autowired
    private MonitorService monitorService;

    //--------------------------------------------------专题集部分------------------------------------------
    //新建专题集
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "insertZTJ")
    public Result insertZTJ(@RequestBody ZTJPo ztjPo,HttpServletRequest request){
        Result result = adminTechnicalBagService.insertZTJ(ztjPo);
        if(result.getCode() == 200){
            try {
                monitorService.record(request,new MonitorTypeTool().getAddFile(),ztjPo.getName(), "专题集库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //修改专题集
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "updateZTJ")
    public Result updateZTJ(@RequestBody ZTJPo ztjPo,HttpServletRequest request){
        Boolean result = null;
        result = adminTechnicalBagService.updateZTJ(ztjPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getEditFile(),ztjPo.getName(), "专题集库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("修改专题集成功");
            }
        }
        return Result.fail("修改专题集失败");
    }

    //删除专题集
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "deleteZTJ")
    public Result deleteZTJ(@RequestBody ZTJPo ztjPo,HttpServletRequest request){
        Boolean result = adminTechnicalBagService.deleteZTJ(ztjPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getDeleteFile(),ztjPo.getName(), "专题集库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("删除专题集成功");
            }
        }
        return Result.fail("删除专题集失败");
    }

    //查询专题集
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "queryZTJ")
    public Result queryZTJ(
            @RequestParam("searchWay") String searchWay,
            @RequestParam("keyWord") String keyWord,
            @RequestParam("page") int page,
            @RequestParam("pageSize") int pageSize
    ){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setSearchWay(searchWay);
        bo.setKeyWord(keyWord);
        bo.setStart((page - 1) * pageSize);
        bo.setEnd(pageSize);
        List<ZTJPo> list = adminTechnicalBagService.queryZTJ(bo);
        Integer total = adminTechnicalBagService.countZTJ(bo).intValue();
        CutPageBean cutPageBean = new CutPageBean<>();
        cutPageBean.initCutPage(total, pageSize, list);
        return Result.succ(cutPageBean);
    }

    //展示专题集封面
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @RequestMapping(value ="/getImage",produces="application/json;charset=UTF-8")
    public Result createFolw(HttpServletRequest request, HttpServletResponse response) {
        String cover = request.getParameter("cover");
        String type = request.getParameter("type");
        if("temp".equals(type)){
            type = coverTemPResource;
        }else{
            type = ztjCoverPath;
        }
        String path = FileUtil.calculateAbstractPath(cover,type);
        byte[] imageBinary = FileuploadUtils.getImageBinary(path);
        return  Result.succ(imageBinary);
    }

    //查询专题集列表
    @GetMapping(value = "queryZTJList")
    public Result queryZTJList(@RequestParam("ztjName") String ztjName){
        List<ZTJPo> list = adminTechnicalBagService.queryZTJList(ztjName);
        return Result.succ(list);
    }

//--------------------------------------------------专题部分------------------------------------------
    //查询专题
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "queryZT")
    public Result queryZT(
            @RequestParam("ztjId") Integer ztjId,
            @RequestParam("searchWay") String searchWay,
            @RequestParam("keyWord") String keyWord,
            @RequestParam("page") int page,
            @RequestParam("pageSize") int pageSize
    ){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setSearchWay(searchWay);
        bo.setKeyWord(keyWord);
        bo.setStart((page - 1) * pageSize);
        bo.setEnd(pageSize);
        bo.setSearchId(ztjId);
        List<ZTPo> list = adminTechnicalBagService.queryZT(bo);
        Integer total = adminTechnicalBagService.countZT(bo).intValue();
        CutPageBean cutPageBean = new CutPageBean<>();
        cutPageBean.initCutPage(total, pageSize, list);
        return Result.succ(cutPageBean);
    }

    //新建专题
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "insertZT")
    public Result insertZT(@RequestBody ZTPo ztPo,HttpServletRequest request){
        Result result = adminTechnicalBagService.insertZT(ztPo);
        if(result.getCode() == 200){
            try {
                monitorService.record(request,new MonitorTypeTool().getAddFile(),ztPo.getName(), "专题库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //修改专题
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "updateZT")
    public Result updateZT(@RequestBody ZTPo ztPo,HttpServletRequest request){
        Result result = adminTechnicalBagService.updateZT(ztPo);
        if(result.getCode() == 200){
            try {
                monitorService.record(request,new MonitorTypeTool().getEditFile(),ztPo.getName(), "专题库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //删除专题
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "deleteZT")
    public Result deleteZT(@RequestBody ZTPo ztPo,HttpServletRequest request){
        Boolean result = adminTechnicalBagService.deleteZT(ztPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getDeleteFile(),ztPo.getName(), "专题库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("删除专题成功");
            }
        }
        return Result.fail("删除专题失败");
    }

    //查询专题列表
    @GetMapping(value = "queryZTList")
    public Result queryZTList(@RequestParam("ztjId") String ztjId,
                              @RequestParam("ztName") String ztName,
                              @RequestParam("ztjName") String ztjName){
        AdminQueryBo bo = new AdminQueryBo();
        if(!"".equals(ztjId) && ztjId != null){
            bo.setSearchWay("专题集ID = " + ztjId);
        }
        if(!"".equals(ztName) && ztName != null){
            bo.setKeyWord("名称 = " + "\"*" + ztName +"\"");
        }
        ZTPo ztPo = new ZTPo();
        ztPo.setId(ztjId);
        ztPo.setName(ztjName);
        List<ZTPo> list = adminTechnicalBagService.queryZTList(bo);
        list.add(ztPo);
        return Result.succ(list);
    }

    //查询专题集专题列表
    @GetMapping(value = "queryZTJZTList")
    public Result queryZTList(@RequestParam("ztjId") String ztjId,
                              @RequestParam("ztName") String ztName){
        AdminQueryBo bo = new AdminQueryBo();
        if(!"".equals(ztjId) && ztjId != null){
            bo.setSearchWay("专题集ID = " + ztjId);
        }
        if(!"".equals(ztName) && ztName != null){
            bo.setKeyWord("名称 = " + "\"*" + ztName +"\"");
        }
        List<ZTPo> list = adminTechnicalBagService.queryZTList(bo);
        return Result.succ(list);
    }

    //--------------------------------------------------专题文献部分------------------------------------------
    //查询专题文献
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "queryZTWX")
    public Result queryZTWX(
            @RequestParam("standardType[]") String[] standardType,
            @RequestParam("searchWay") String searchWay,
            @RequestParam("keyWord") String keyWord,
            @RequestParam("ztjId") String ztjId,
            @RequestParam("ztId") String ztId,
            @RequestParam("page") int page,
            @RequestParam("pageSize") int pageSize
    ){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setSearchWay(searchWay);
        bo.setKeyWord(keyWord);
        bo.setStart((page - 1) * pageSize);
        bo.setEnd(pageSize);
        bo.setZtId(ztId);
        bo.setZtjId(ztjId);
        bo.setStandardType(standardType);
        List<ZTWXPo> list = adminTechnicalBagService.queryZTWX(bo);
        Integer total = adminTechnicalBagService.countZTWX(bo).intValue();
        CutPageBean cutPageBean = new CutPageBean<>();
        cutPageBean.initCutPage(total, pageSize, list);
        return Result.succ(cutPageBean);
    }

    //新建专题文献
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "insertZTWX")
    public Result insertZTWX(@RequestBody ZTWXPo ztwxPo,HttpServletRequest request){
        Boolean result = null;
        result = adminTechnicalBagService.insertZTWX(ztwxPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getAddFile(),ztwxPo.getTitle(), "专题文献库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("新建专题文献成功");
            }
        }
        return Result.fail("新建专题文献失败");
    }

    //修改专题文献
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "updateZTWX")
    public Result updateZTWX(@RequestBody ZTWXPo ztwxPo,HttpServletRequest request){
        Boolean result = null;
        result = adminTechnicalBagService.updateZTWX(ztwxPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getEditFile(),ztwxPo.getTitle(), "专题文献库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("修改专题文献成功");
            }
        }
        return Result.fail("修改专题文献失败");
    }

    //删除专题文献
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "deleteZTWX")
    public Result deleteZTWX(@RequestBody ZTWXPo ztwxPo,HttpServletRequest request){
        Boolean result = adminTechnicalBagService.deleteZTWX(ztwxPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getDeleteFile(),ztwxPo.getTitle(), "专题文献库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("删除专题文献成功");
            }
        }
        return Result.fail("删除专题文献失败");
    }

    //查询专题文献最大序号
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "getMaxZTWXId")
    public Result getMaxZTWXId(
            @RequestParam("ztjId") String ztjId,
            @RequestParam("ztId") String ztId
    ){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setZtId(ztId);
        bo.setZtjId(ztjId);
        bo.setStart(0);
        bo.setEnd(1);
        List<ZTWXPo> list = adminTechnicalBagService.getMaxZTWXId(bo);
        return Result.succ(list);
    }

}
